import router from '../router'


// import { getSeckillGoodsList } from '@/api/api'

import { getBanner, getGoodsList, getSeckillGoodsList } from '@/api/api'
import { ref, computed, onMounted, Ref, h } from 'vue';



export function useHomeNav() {
    const nav = [{
        id: '',
        text: '京东超市',
        url:
            'https://m15.360buyimg.com/mobilecms/jfs/t1/175540/24/19329/6842/60ec0b0aEf35f7384/ec560dbf9b82b90b.png!q70.jpg',
    },
    {
        id: '',
        text: '数码电器',
        url:
            'https://m15.360buyimg.com/mobilecms/jfs/t1/178015/31/13828/6862/60ec0c04Ee2fd63ac/ccf74d805a059a44.png!q70.jpg',
    },
    {
        id: '',
        text: '京东百货',
        url:
            'https://m15.360buyimg.com/mobilecms/jfs/t1/54043/33/19389/4660/62b049dbE3b9aef75/2fcd31afd5d702e4.png!q70.jpg',
    },
    {
        id: '',
        text: '京东生鲜',
        url:
            'https://m15.360buyimg.com/mobilecms/jfs/t1/177902/16/13776/5658/60ec0e71E801087f2/a0d5a68bf1461e6d.png!q70.jpg',
    },
    {
        id: '',
        text: '京东到家',
        url:
            'https://m15.360buyimg.com/mobilecms/jfs/t1/196472/7/12807/7127/60ec0ea3Efe11835b/37c65625d94cae75.png!q70.jpg',
    },
    {
        id: '',
        text: '京东超市',
        url:
            'https://m15.360buyimg.com/mobilecms/jfs/t1/175540/24/19329/6842/60ec0b0aEf35f7384/ec560dbf9b82b90b.png!q70.jpg',
    },
    {
        id: '',
        text: '数码电器',
        url:
            'https://m15.360buyimg.com/mobilecms/jfs/t1/178015/31/13828/6862/60ec0c04Ee2fd63ac/ccf74d805a059a44.png!q70.jpg',
    },
    {
        id: '',
        text: '京东百货',
        url:
            'https://m15.360buyimg.com/mobilecms/jfs/t1/54043/33/19389/4660/62b049dbE3b9aef75/2fcd31afd5d702e4.png!q70.jpg',
    },
    {
        id: '',
        text: '京东生鲜',
        url:
            'https://m15.360buyimg.com/mobilecms/jfs/t1/177902/16/13776/5658/60ec0e71E801087f2/a0d5a68bf1461e6d.png!q70.jpg',
    },
    {
        id: '',
        text: '京东到家',
        url:
            'https://m15.360buyimg.com/mobilecms/jfs/t1/196472/7/12807/7127/60ec0ea3Efe11835b/37c65625d94cae75.png!q70.jpg',
    },
    ]

    const changeNav = (id: any) => {
        router.push({ name: 'cart', query: { id } })
    }


    return {
        nav,
        changeNav
    }
}


export function useHomeSwiper() {
    const bannerList: any = ref([])
    onMounted(async () => {
        const res = await getBanner()
        bannerList.value = res.data
    })


    return bannerList



}


//  首页商品列表hooks
export function useHomeGoodsList() {

    //商品列表
    const goodsList: any = ref([])
    onMounted(async () => {
        const res = await getGoodsList()
        goodsList.value = res.data
    })


    return goodsList






}


//  倒计时  
// 方式一  以秒杀进行计算
// 倒计时 肯定是一个动态 
//  假设 离 结束时间还有2小时
// 2小时是当前时间的后 2小时 
// 1 获取当前时间    new  Date()
// 2 当前时间的后2小时    2*60*60*1000+     new  Date()
// 3 当前时间转换成 时间戳    new  Date().getTime()



// 方式二   以小时为单位进行计算


export function useSeckillGoods() {

    // 倒计时
    function useTimeBack() {

        const _time = ref<any>(20); //初始值
        const time = ref(0)


        const date = new Date()

        let year = date.getFullYear()
        let month = date.getMonth() + 1
        let day = date.getDate()
        let h = date.getHours()
        let mm = date.getMinutes()
        let ss = date.getSeconds()

        // 比当前时间 多2小时
        if (h % 2 == 0) {
            h += 2
        } else {
            h += 1
        }

        _time.value = h - 2;


        // 做判断
        // 判断 小时    引发天数  31   引发 月份 
        if (h >= 24) {
            h = 0;
            day += 1

            // 大小月 份
            //计算1 3 5 7 8 10 12  
            let arr = [1, 3, 5, 7, 8, 10, 12]


            // 第一个条件是  小月份 但是是31号  结果是下个月的第1天                  // 31号 是大月份   下个月的1号
            if ((day === 31 && [1, 3, 5, 7, 8, 10, 12].indexOf(month) === -1) || (day === 32 && [1, 3, 5, 7, 8, 12].indexOf(month) !== -1)) {
                day = 1;
                month += 1
                if (month === 13) {
                    month = 1;
                    year += 1;
                }
            }

        }




        console.log(new Date(`${year}/${month}/${day} ${h}:00:00`).getTime())

        console.log(date.getTime())


        let endTime = new Date(`${year}/${month}/${day} ${h}:00:00`).getTime()
        let startTime = date.getTime()

        let res = endTime - startTime
        console.log(res)
        time.value = res

        return time


    }


    // 获取秒杀商品
    // 1 发送请求 
    // 2 数据赋值  
    //  3 数据导出

    function useSeckGoods() {
        const seckillList = ref([])
        //  获取商品 
        onMounted(async () => {
            const res = await getSeckillGoodsList()
            seckillList.value = res.data
        })


        // const currentActive = (index) => {
        // }



        return seckillList
    }


    const time = useTimeBack()

    const seckillList = useSeckGoods()



    // formatTime  函数是 在 小时 或者分钟 或者 秒 前面 如果小于10 补0

    const formatTime = (data: any) => {
        return data < 10 ? '0' + data : data
    }



    return {
        time,
        seckillList,
        formatTime
    }

}






